//
//  ORTouTableViewCell.m
//  News
//
//  Created by rimi on 2016/12/11.
//  Copyright © 2016年 dongfumei. All rights reserved.
//

#import "ORTouTableViewCell.h"

@interface ORTouTableViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *type;
@property (nonatomic, strong) UIImageView *img1;
@property (nonatomic, strong) UIImageView *img2;
@property (nonatomic, strong) UIImageView *img3;

@property (nonatomic, strong) NSArray *imgArray;

@end

@implementation ORTouTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.titleLabel];
        
        for (int i = 0; i < 3; i++) {
            UIImageView *imgV = [[UIImageView alloc]initWithFrame:AAdaptionRect(20+(kBaseWidth - 20)/3*i, CGRectGetMaxY(self.type.frame)/AAdaptationWidth()+10, 110, 90)];
            [imgV sd_setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"4"]];
            [self.contentView addSubview:imgV];
            if (i == 0) {
                self.img1 = imgV;
            }else if (i ==1){
                self.img2 = imgV;
            }else{
                self.img3 = imgV;
            }
        }
        
        [self.contentView addSubview:self.type];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    NSString *text = self.titleLabel.text;
    CGSize textSize = [ORTouTableViewCell sizeText:text];
    self.titleLabel.frame = AAdaptionRect(20, CGRectGetMaxY(self.img1.frame)/AAdaptationWidth()+15, kBaseWidth - 40, textSize.height);
}

+(CGSize) sizeText:(NSString *)text{
    CGRect textRect = [text boundingRectWithSize:CGSizeMake(kBaseWidth - 40, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:AAFont(16)} context:nil];
    return textRect.size;
}


+(CGFloat)cellHeight:(NSString *)text{
    
    CGFloat height = [ORTouTableViewCell sizeText:text].height;
    return AAdaptation(140) + height;
}

-(void)withDataModel:(ORTouModel *)model{
    self.titleLabel.text = model.title;
    NSMutableString *strInfo = [NSMutableString string];
    if (model.type) {
        [strInfo appendFormat:@"#%@# ",model.type];
    }
    if (model.realtype) {
        [strInfo appendFormat:@"#%@# ",model.realtype];
    }
    [strInfo appendFormat:@"%@", model.author_name];
    [strInfo appendFormat:@" %@",model.date];
    self.type.text = strInfo;
    [self.img1 sd_setImageWithURL:[NSURL URLWithString:model.thumbnail_pic_s] placeholderImage:[UIImage imageNamed:@"4"]];
    [self.img2 sd_setImageWithURL:[NSURL URLWithString:model.thumbnail_pic_s02] placeholderImage:[UIImage imageNamed:@"4"]];
    [self.img3 sd_setImageWithURL:[NSURL URLWithString:model.thumbnail_pic_s03] placeholderImage:[UIImage imageNamed:@"4"]];
    
}

-(UILabel *)type{
    if (!_type) {
        _type = [[UILabel alloc]initWithFrame:AAdaptionRect(20, 10, kBaseWidth - 20, 20)];
        _type.font = AAFont(15);
        _type.textColor = MainColor;
    }
    return _type;
}


-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:AAdaptionRect(20, CGRectGetMaxY(self.img1.frame)/AAdaptationWidth()+35, kBaseWidth - 40, 0)];
        _titleLabel.font = AAFont(16);
        _titleLabel.numberOfLines = 2;
    }
    return _titleLabel;
}

@end
